package cn.mw.cmdb.monitorview.service;

import cn.mw.cmdb.monitorview.entity.MwBladeDto;
import cn.mw.cmdb.param.zabbixResult.ItemApplication;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gengjb
 * @description TODO
 * @date 2024/8/30 16:28
 */
public class BladeHandler {

    private final String CPU_MODEL = "CPU_MODEL";
    private final String MEMORY_STATE = "MEMORY_STATE";
    private final String DISK_STATE = "DISK_STATE";
    private final String FAN_STATE = "FAN_STATE";
    private final String POWER_STATE = "POWER_STATE";
    private final String TEMPERATURE_STATE = "TEMPERATURE_STATE";
    private final String DISK_ARRAY_MODEL = "DISK_ARRAY_MODEL";

    public void handler(MwBladeDto bladeDto, List<ItemApplication> applications){
        //CPU
        List<ItemApplication> cpuItems = applications.stream().filter(item -> item.getName().contains(CPU_MODEL)).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(cpuItems)){
            bladeDto.setCpu(cpuItems.get(0).getLastvalue());
        }
        //阵列卡
        List<ItemApplication> arrayModels = applications.stream().filter(item -> item.getName().contains(DISK_ARRAY_MODEL)).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(arrayModels)){
            List<String> names = arrayModels.stream().filter(item->StringUtils.isNotEmpty(item.getLastvalue())).map(item -> item.getLastvalue()).collect(Collectors.toList());
            bladeDto.setArrayCard(names);
        }
        //内存
        List<ItemApplication> memoryItems = applications.stream().filter(item -> item.getName().contains(MEMORY_STATE)).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(memoryItems)){
            int normal = 0;
            int abNormal = 0;
            for (ItemApplication memoryItem : memoryItems) {
                String lastvalue = memoryItem.getLastvalue();
                if(StringUtils.isNotEmpty(lastvalue) && Double.parseDouble(lastvalue) == 3){
                    normal++;
                }else{
                    abNormal++;
                }
            }
            bladeDto.setMemoryTotal(normal+abNormal);
            bladeDto.setMemoryNormalNumber(normal);
            bladeDto.setMemoryAbNormalNumber(abNormal);
        }

        //磁盘
        List<ItemApplication> diskItems = applications.stream().filter(item -> item.getName().contains(DISK_STATE)).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(diskItems)){
            int normal = 0;
            int abNormal = 0;
            for (ItemApplication diskItem : diskItems) {
                String lastvalue = diskItem.getLastvalue();
                if(StringUtils.isNotEmpty(lastvalue) && Double.parseDouble(lastvalue) == 3){
                    normal++;
                }else{
                    abNormal++;
                }
            }
            bladeDto.setDiskTotal(normal+abNormal);
            bladeDto.setDiskNormalNumber(normal);
            bladeDto.setDiskAbNormalNumber(abNormal);
        }

        //风扇
        List<ItemApplication> fanItems = applications.stream().filter(item -> item.getName().contains(FAN_STATE)).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(fanItems)){
            int normal = 0;
            int abNormal = 0;
            for (ItemApplication fanItem : fanItems) {
                String lastvalue = fanItem.getLastvalue();
                if(StringUtils.isNotEmpty(lastvalue) && Double.parseDouble(lastvalue) == 3){
                    normal++;
                }else{
                    abNormal++;
                }
            }
            bladeDto.setFanTotal(normal+abNormal);
            bladeDto.setFanNormalNumber(normal);
            bladeDto.setFanAbNormalNumber(abNormal);
        }

        //电源
        List<ItemApplication> powerItems = applications.stream().filter(item -> item.getName().contains(POWER_STATE)).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(powerItems)){
            int normal = 0;
            int abNormal = 0;
            for (ItemApplication powerItem : powerItems) {
                String lastvalue = powerItem.getLastvalue();
                if(StringUtils.isNotEmpty(lastvalue) && Double.parseDouble(lastvalue) == 3){
                    normal++;
                }else{
                    abNormal++;
                }
            }
            bladeDto.setPowerSupplyTotal(normal+abNormal);
            bladeDto.setPowerSupplyNormalNumber(normal);
            bladeDto.setPowerSupplyAbNormalNumber(abNormal);
        }

        //电源
        List<ItemApplication> temperatureItems = applications.stream().filter(item -> item.getName().contains(TEMPERATURE_STATE)).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(temperatureItems)){
            int normal = 0;
            int abNormal = 0;
            for (ItemApplication temperatureItem : temperatureItems) {
                String lastvalue = temperatureItem.getLastvalue();
                if(StringUtils.isNotEmpty(lastvalue) && Double.parseDouble(lastvalue) == 3){
                    normal++;
                }else{
                    abNormal++;
                }
            }
            bladeDto.setTemperatureTotal(normal+abNormal);
            bladeDto.setTemperatureNormalNumber(normal);
            bladeDto.setTemperatureAbNormalNumber(abNormal);
        }
    }
}
